<?php
include("friendfeed.php");
include("JSON.php");

$settingsFile = dirname(__FILE__)."/frindosettings.php";
$runConfig = false;

// only process the configuration form if the settings file doesn't exist
if (!is_file($settingsFile) && isset($_GET['processConfig']))
{
	if ($fp = fopen($settingsFile, 'w'))
	{
		fwrite($fp, '<?php'."\r\n");
		fwrite($fp, '$ffID="'.str_replace(";", "", $_GET['ffID']).'";'."\r\n");
		fwrite($fp, '$ffKey="'.str_replace(";", "", $_GET['ffKey']).'";'."\r\n");
		//fwrite($fp, '$numEntries='.str_replace(";", "", $_GET['numEntries']).';'."\r\n");
		fwrite($fp, '$cullTweets='.(isset($_GET['cullTweets'])?'true':'false').';'."\r\n");
		fwrite($fp, '?>'."\r\n");
		fclose($fp);
		
		?>
		Your settings have been saved!
		<?php
	}
	else
	{
		?>
		<font color=red>Could not write settings file! (make sure the frindo folder is writable for everyone during this process)</font>
		<?php	
	}
}

if (is_file($settingsFile))
{
	include($settingsFile);
}
else
{
	echo "Could not find settings file:".$settingsFile." Running config...<br/>";
	$runConfig = true;
}	

// if we need to run the config, then show the form
if ($runConfig)
{
?>

<html><head><title>Frindo</title></head><body>
	
<h1>Frindo!</h1><br/>
<p>Frindo is a simple PHP script that takes your FriendFeed and allows you to embed it on your site in a customizable way. Feel free to modify the code any way you see fit. </p>

<B>PHP Usage</B><br/>
If you want to embed this file in another PHP file. Put this in that PHP file. Make sure the path to the frindo folder is correct:<br/>
&lt;?php include_once('frindo/index.php'); ?&gt;<br/><br/>

<B>Widget Usage</B><br/>
To embed this as a widget put this in your HTML code:<br/>
&lt;iframe src='frindo/index.php'&gt;&lt;/iframe&gt;<br/><br/>

<B>CSS</B><br/>
Once you have Frindo configured and running, the best thing to do is view the generated source code and see what CSS identifiers are 
being used and make a stylesheet to define those styles

<br/><br/>

<B>Settings</B><br/>
<form action=./ method=get>
<table>
	<td align=right>FriendFeed ID:</td><td><input type=text name=ffID value='<?php echo $ffID;?>'></td><tr/>
	<td align=right>API Key [<a href=https://friendfeed.com/account/api>?</a>]:</td><td><input type=text name=ffKey value='<?php echo $ffKey;?>'></td><tr/>	
	<!-- <td align=right>Number of Entries To Show:</td><td><input type=text name=numEntries value='<?php echo $numEntries;?>'></td><tr/>-->
	<td align=right>Ignore Tweets?</td><td><input type=checkbox name=cullTweets <?php echo $cullTweets?'checked':'';?>> Yes, please.</td><tr/>	
	<input type=hidden name=processConfig value=1>
	<input type=hidden name=config value=1>
	<td colspan=2><center><input type=submit></center></td>
</table>
</form>	
	
</body>
</html>
	
<?php
// short cicuit
return;
}
?>

<!-- script code to hide/unhide the comments on entries -->
<script language=javascript type='text/javascript'>
function toggleVis(id) {
	var e = document.getElementById(id)
	e.style.visibility = e.style.visibility=='hidden'? 'visible' : 'hidden';
	e.style.display = e.style.visibility=='hidden'? 'none' : 'block';
}
</script>


<div class="feedheader"><a href="http://friendfeed.com/<?php print $ffID; ?>" target=_new><strong>Stream</strong></a>  [<a target=_new href="http://friendfeed.com/<?php print $ffID; ?>?format=atom">feed</a>] [<a target=_new href="http://friendfeed.com/<?php print $ffID; ?>">link</a>]</div>



<?php
//Connect
$friendfeed = new FriendFeed($ffID, $ffKey);
// Some breadcrumb variables
$commentID = 0;
$mediaPlayerID = 0;
$currDate = 0;
$output = '';


// Pull the entries from FF. Have to loop to emulate pagination
// since it appears that anything above 30 for fetch_user_feed is apparently ignored!


for ($i = 0; $i < 2; $i++)
{
	$feed = $friendfeed->fetch_user_feed($ffID, null, $i * 30, ($i + 1) * 30 );
	foreach ($feed->entries as $entry) {


		if ($cullTweets && $entry->service->id == 'twitter')
			continue;

		// the time/date functions on my server are wonky and this is the best
		// way to process the publish date since I'm only using it to display
		// a little header
		$dateFmt = date("F j, Y", $entry->published);// substr($entry->published, 0, 10);
		if (strcmp($dateFmt, $currDate) != 0)
		{
			echo '<h1>'.$dateFmt.'</h1>';
			$currDate = $dateFmt;	
		}

		// create a div for this entry
		echo '<div class=feedentry><div class="feedEntryContents">'.$entry->title.' [<a target=_new href="'.$entry->link.'">link</a>]';

		// if there are media objects attached, publish the thumbnails and any 'player' objects
		if (count($entry->media) > 0)
		{
			foreach ($entry->media as $media)	
			{
				echo '<div style="float: left" class="mediaDiv">';
				if (count($media->thumbnails) > 0)
				{
					$thumb = $media->thumbnails[0];
					//foreach ($media->thumbnails as $thumb)	
					{
						echo '<a target=_new href="'.$entry->link.'"><img src='.$thumb->url.' border=0></a>';
					}
				}

			
			   /*
				if (isset($media->player))
				{
					echo '<a href="javascript:toggleVis(\'mediaPlayer-'.$mediaPlayerID.'\');">Show Media Player</a><br/>';
					echo '<div id="mediaPlayer-'.$mediaPlayerID.'" style="width: 90%; margin-left: 10%; visibility: hidden; display: none">';
					echo $media->player;
					//echo '</div>';
					$mediaPlayerID++;
				}
				*/
				

				echo '</div>';
			}
		}

		echo '<br clear="all"><i>(via <a href="'.$entry->service->profileUrl.'"><img src="'.$entry->service->iconUrl.'"/></a>)</i> ';
		
		// end of a single entry contents
		echo '</div>';

		// show the likes for the entry
		$likeCnt = count($entry->likes);
		if ($likeCnt > 0)
		{
			foreach ($entry->likes as $like)
			{
				$likeCnt--;
				echo '<a href='.$like->user->profileUrl.'>'.$like->user->name.($likeCnt>0?', ':'').'</a>';
			}
			echo ' Liked It. ';
		}

		// show the comment count and build a collapsible div
		if (count($entry->comments) > 0)
		{
			echo '<a href="javascript:toggleVis(\'comment-'.$commentID.'\');">'.count($entry->comments).' Comments</a><br/>';
			echo '<div id="comment-'.$commentID.'" style="width: 90%; margin-left: 10%; visibility: hidden; display: none">';
			foreach($entry->comments as $comment)
				echo '<a href='.$comment->user->profileUrl.'>'.$comment->user->name.'</a>: "'.$comment->body.'"<hr noshade>';
			echo '</div>';

			$commentID++;
		}

		// end of a single entry div
		echo '</div>';
	}
}
?>


